package cn.hl.hm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.hl.hm.constants.OrderStatus;
import cn.hl.hm.constants.PayMethod;
import cn.hl.hm.entity.Order;
import cn.hl.hm.entity.RechargeRecoard;
import cn.hl.hm.handler.PayExecuteFactory;
import cn.hl.hm.handler.PayHandler;
import cn.hl.hm.service.OrderService;
import cn.hl.hm.service.RechargeRecoardService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;

/**
 * 处理支付请求（验证支付方式，通过后签）
 * 
 * @author 大和
 * 
 * 支付方式：30、微信 31、支付宝 
 */
@Controller
@RequestMapping("/rechargePayController")
public class RoadPrePayController {
	@Autowired
	RechargeRecoardService rechargeRecoardService;

	@RequestMapping(value = "/prepay", method = RequestMethod.POST)
	public String prepay(@RequestParam("payMethod") String payMethod, @RequestParam("rechargeCode") String rechargeCode,
			String orderInfo, // 支付宝签名时候必填
			@RequestParam("iosFlag") String iosFlag,// 1 企业版 2正式  安卓始终1

			HttpServletRequest request, HttpServletResponse response) {
		try {
			//阿里支付 必填校验
			if (payMethod.equals(PayMethod.ROAD_ZFB_RECHARGE.getCode()) && StringUtils.isBlank(orderInfo)) {
				Result.ok(null, InfoCode.PARAM_ERROR, response);
				return null;
			}
			if (payMethod.equals(PayMethod.ROAD_ZFB_RECHARGE.getCode())||payMethod.equals(PayMethod.ROAD_WX_RECHARGE.getCode())){
			
			}else {
				Result.ok(null, InfoCode.PAY_METHOD_ERROR, response);
				return null;
			}
			PayMethod method = PayMethod.getPayMethod(payMethod);
			if (method == null) {
				Result.ok(null, InfoCode.PAY_METHOD_ERROR, response);
				return null;

			}
			//重复支付
			RechargeRecoard orderParam = new RechargeRecoard();
			orderParam.setRechargeCode(rechargeCode);
			List<RechargeRecoard> selectOrder = rechargeRecoardService.selectRechargeRecoard(orderParam);
			RechargeRecoard order = null;
			if(selectOrder!=null&&selectOrder.size()>0){
				order = selectOrder.get(0);
				if(!order.getStatus().equals("1")){
					Result.ok(null, InfoCode.PAIED_ERROR, response);
					return null;
				}
			}else {
				Result.ok(null, InfoCode.PAY_ORDER_ERROR, response);
				return null;
			}
			
			
			PayHandler payHandler = PayExecuteFactory.findPayHandler(method);
			if (payHandler == null) {
				Result.ok(null, InfoCode.PAY_HANDLER_ERROR, response);
				return null;

			}
			Map<String, String> param = new HashMap<>();
			param.put("payMethod", payMethod);
			param.put("rechargeCode", rechargeCode);
			param.put("orderInfo", orderInfo);
			param.put("iosFlag", iosFlag);

			Map<String, String> handler = payHandler.handler(param);
			Result.ok(handler, InfoCode.SUCCESS, response);
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			
		}
		return null;

	}

}
